package com.xiaozeng.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result implements Serializable {
    public static final Long serialVersionUID = 1L;
    /**
     * 通用成功状态码
     */
    public static final String SUCCESS = "200";
    /**
     * 失败状态码
     */
    public static final String FAIL = "400";
    /**
     * 未登录
     */
    public static final String USER_NO_LOGIN = "401";

    /**
     * Result类型，封装到Json中
     */
    private String code;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String msg;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Object data;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long   total;


    /**
     * 成功
     * @param msg:提示
     * @param data：数据
     */
    public static Result ok(String msg,Object data,Long total){
        return new Result(SUCCESS,msg,data,total);
    }

    /**
     * 成功
     */
    public static Result ok(String msg,Object data){
        return ok(msg,data,null);
    }

    /**
     * 成功
     */
    public static Result ok(String msg){
        return ok(msg,null);
    }


    /**
     * 成功
     */
    public static Result ok(Object data){
        return ok(null,data);
    }



    /**
     * 失败：有total版
     */
    public static Result fail(String msg,Object data,Long total){
        return new Result(FAIL,msg,data,total);
    }

    /**
     * 失败：有data版
     */
    public static Result fail(String msg,Object data){
        return fail(msg,data,null);
    }

    /**
     * 失败：只有msg版
     */
    public static Result fail(String msg){
        return fail(msg,null);
    }


    /**
     * 失败：啥都没有版
     */
    public static Result fail(Object data){
        return fail(null,data);
    }


    /**
     * 未登录
     */
    public static Result NO_LOGIN(){
        return fail(USER_NO_LOGIN,"用户未登录!");
    }


}
